import * as types from "../types/placeRecycleOrderTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import { isEmpty } from "../utils/utils";
import ApiConstants from "../common/ApiConstants";
import { toastShort } from "../utils/ToastUtils";
import RegexUtil from "../utils/RegexUtil";

export function placeRecycleOrder(
  recycleOrderPlan,
  contact_name,
  collection_mobile,
  card_no,
  collection_type,
  alipay_no,
  alipay_name,
  weichat_name,
  weichat_no,
  callback,
) {
  return dispatch => {
    if (isEmpty(contact_name)) {
      toastShort("请输入联系人姓名");
      return;
    }
    if (isEmpty(collection_mobile)) {
      toastShort("请输入联系人手机号");
      return;
    }
    if (!RegexUtil.checkMobile(collection_mobile)) {
      toastShort("请输入正确的联系人手机号");
      return;
    }
    if (isEmpty(card_no)) {
      toastShort("请输入联系人身份证号");
      return;
    }
    if (collection_type === 1) {
      if (isEmpty(alipay_no)) {
        toastShort("请输入支付宝收款账号");
        return;
      }
      if (isEmpty(alipay_name)) {
        toastShort("请输入支付宝收款姓名");
        return;
      }
    } else if (isEmpty(weichat_name)) {
      toastShort("请输入微信收款姓名");
      return;
    } else if (isEmpty(weichat_no)) {
      toastShort("请输入微信收款账号");
      return;
    }

    dispatch({ type: types.PLACERECYCLEORDER_LOADING });
    const { facility_recyle, facility_recyle_order } = recycleOrderPlan;
    const params = {
      facility_recycle: facility_recyle,
      facility_recyle_order,
      contact_name,
      collection_mobile,
      card_no,
      collection_type,
    };
    if (collection_type === 1) {
      // 支付宝
      params.collection_no = alipay_no;
      params.collection_name = alipay_name;
    } else {
      // 微信
      params.collection_no = weichat_no;
      params.collection_name = weichat_name;
    }
    NetWorkUtil.post(ApiConstants.RECYCLE_CREATEORDER, params)
      .then(res => {
        toastShort("下单成功");
        dispatch({ type: types.PLACERECYCLEORDER_DOWN });
        callback(res);
      })
      .catch(error => {
        toastShort(error.message);
        dispatch({ type: types.PLACERECYCLEORDER_ERROR, error });
      });
  };
}

export function inputContractor(contractor) {
  return dispatch => {
    dispatch({ type: types.CONTRACTOR_CHANGE, contractor });
  };
}

export function inputMobile(mobile) {
  return dispatch => {
    dispatch({ type: types.MOBILE_CHANGE, mobile });
  };
}

export function inputIdCard(idCard) {
  return dispatch => {
    dispatch({ type: types.IDCARD_CHANGE, idCard });
  };
}

export function inputAlipayName(alipay_name) {
  return dispatch => {
    dispatch({ type: types.ALIPAY_NAME_CHANGE, alipay_name });
  };
}

export function inputAlipayNo(alipay_no) {
  return dispatch => {
    dispatch({ type: types.ALIPAY_NUMBER_CHANGE, alipay_no });
  };
}

export function inputWeiChatName(weichat_name) {
  return dispatch => {
    dispatch({ type: types.WEICHATPAY_NAME_CHNAGE, weichat_name });
  };
}

export function inputWeiChatNo(weichat_no) {
  return dispatch => {
    dispatch({ type: types.WEICHATPAY_NUMBER_CHNAGE, weichat_no });
  };
}

export function changeCollectionType(collectionType) {
  return dispatch => {
    dispatch({ type: types.COLLECTIONTYPE_CHANGE, collectionType });
  };
}

export function unmount() {
  return dispatch => {
    dispatch({ type: types.PLACERECYCLEORDER_UNMOUNT });
  };
}
